package com.kaluli.project.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.phprpc.util.AssocArray;
import org.phprpc.util.PHPSerializer;

public class SerializeUtil {
	
	
	
	
	public static Map<String, String> serialize(String serialize){
		PHPSerializer p = new PHPSerializer();
		AssocArray array = null;
		Map<String, String> map = new HashMap<>();
		try {
			array = (AssocArray) p.unserialize(serialize.getBytes());
			Map<String, String> m = array.toHashMap();
		
			// for()
			for (String key : m.keySet()) {
				map.put(key, new String((byte[])(array.get(key))));
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
		
	}
	
	public static Map<Integer, String> serializeInt(String serialize){
		PHPSerializer p = new PHPSerializer();
		AssocArray array = null;
		Map<Integer, String> map = new HashMap<>();
		try {
			array = (AssocArray) p.unserialize(serialize.getBytes());
			Map<Integer, String> m = array.toHashMap();
		
			// for()
			for (Integer key : m.keySet()) {
				map.put(key, new String((byte[])(array.get(key))));
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
		
	}
	
	public static Map<String, String> serializeAttr(String serialize){
		PHPSerializer p = new PHPSerializer();
		AssocArray array = null;
		Map<String, String> map = new HashMap<>();
		try {
			array = (AssocArray) p.unserialize(serialize.getBytes());
			AssocArray arrayAttr = (AssocArray) array.get("attr");
			Map<String, String> m = arrayAttr.toHashMap();
		
			// for()
			for (String key : m.keySet()) {
				map.put(key, new String((byte[])(arrayAttr.get(key))));
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
		
	}

	public static void main(String[] args) throws IllegalArgumentException, InvocationTargetException {
		String str = "a:3:{i:0;s:30:\"京东同步物流单号操作\";i:1;N;i:2;N;}";
		String s = "{\"qq\",\"ww\"}";
		PHPSerializer p = new PHPSerializer();
		//  a:6:{s:12:"适用人群";s:23:"18岁以上减肥人群";s:12:"产品剂型";s:6:"胶囊";s:12:"计价单位";s:3:"粒";s:6:"品牌";s:12:"肌肉科技";s:6:"产地";s:6:"美国";s:6:"规格";s:6:"100粒";}
//		String str = "a:6:{s:12:\"适用人群\";s:23:\"18岁以上运动人群\";s:12:\"产品剂型\";s:6:\"粉剂\";s:12:\"计价单位\";s:3:\"磅\";s:6:\"品牌\";s:12:\"肌肉科技\";s:6:\"产地\";s:6:\"美国\";s:6:\"规格\";s:9:\"5磅/2磅\";}";
		try {
			System.out.println(new String(p.serialize(s.getBytes())));
			AssocArray map2 = (AssocArray) p.unserialize(str.getBytes());
			Map<String, String> al = map2.toHashMap();
			// for()
			for (String key : al.keySet()) {
				System.out.println("key= " + key + " and value= " + new String((byte[])(map2.get(key))));
			}
			//
			// System.out.println(((AssocArray)(map3.get("规格"))).);

			// for (Object s : map2.) {
			// System.out.println("key:"+s+" 	 value:"+map2.get(s));
			// }
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
